<template>
  <div>
    <button @click="download">下载xlsx文件</button>
  <div ref="container" style="width: 100%; height: 400px;">    
  </div>
  </div>
</template>
 
<script>
import { createUniver, defaultTheme, LocaleType, merge } from '@univerjs/presets';
import { UniverSheetsCorePreset } from '@univerjs/presets/preset-sheets-core';
import UniverPresetSheetsCoreZhCN from '@univerjs/presets/preset-sheets-core/locales/zh-CN';
 
import '@univerjs/presets/lib/styles/preset-sheets-core.css';
 
import { UniverSheetsAdvancedPreset,downloadFile } from '@univerjs/presets/preset-sheets-advanced';
import UniverPresetSheetsAdvancedZhCN from '@univerjs/presets/preset-sheets-advanced/locales/zh-CN';
import { UniverSheetsDrawingPreset } from '@univerjs/presets/preset-sheets-drawing'
import UniverPresetSheetsDrawingZhCN from '@univerjs/presets/preset-sheets-drawing/locales/zh-CN'
 
import '@univerjs/presets/lib/styles/preset-sheets-drawing.css'
import '@univerjs/presets/lib/styles/preset-sheets-advanced.css'
//excel的导出
//  https://docs.univer.ai/zh-CN/guides/sheets/features/import-export#%E9%80%9A%E8%BF%87-workbook-%E6%95%B0%E6%8D%AE%E5%AF%BC%E5%87%BA-xlsx
//preset和@univerjs-pro/exchange-client不能混用
//import { downloadFile } from '@univerjs-pro/exchange-client';

export default {
  data() {
    return {
      univerAPI: null,
    };
  },
  methods: {
    async download() {
      const fWorkbook = this.univerAPI.getActiveWorkbook();
      const snapshot = fWorkbook.save();
      const file = await this.univerAPI.exportXLSXBySnapshotAsync(snapshot);
      console.log('exportXLSXBySnapshotAsync', file);
      // 下载文件
      downloadFile(file, 'univer', 'xlsx');
    },
  },
  mounted() {
    //2025-5过期 let lic = '1846850823316525148-1-eyJpIjoiMTg0Njg1MDgyMzMxNjUyNTE0OCIsInYiOiIxIiwicCI6ImttLzNld3pxc01KYm91Q1VtbmtlWUtocEd5a1d2a1pPM0Fnb0xrYjVjL3M9IiwicnQiOjMsImZ0Ijp7InVmIjp7Im11IjoyMTQ3NDgzNjQ2LCJldCI6MTc0NTkxMjEyNiwibW0iO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OjE3NTAzODY1NDEsImF0IjoxNzQ3Nzk0NTQxLCJlIjoiNTIzMzc5ODQ1QHFxLmNvbSIsImQiOjgsIm4iOjQ5fQ==-m7p8kJyaGpHjVf0os4bh6DX+iCatrsQCD9wAOrCOqyO5hIQmhvlDcO7/cpam5hMWZKmTBdVEKJ7K249i6J9CCQ==-1750386541'
    console.log('mounted', lic);

    const { univerAPI } = createUniver({
      locale: LocaleType.ZH_CN,
      locales: {
        [LocaleType.ZH_CN]: merge(
          {},
          UniverPresetSheetsCoreZhCN,
          UniverPresetSheetsDrawingZhCN,
          UniverPresetSheetsAdvancedZhCN,
        ),
      },
      theme: defaultTheme,
      presets: [
        UniverSheetsCorePreset({
          container: this.$refs.container,
        }),
        UniverSheetsDrawingPreset(),
        UniverSheetsAdvancedPreset({
          universerEndpoint: 'http://localhost:8000',
          license: lic,
        }),
      ],
    });
    
    univerAPI.createWorkbook({ name: 'Test Sheet' });
    this.univerAPI = univerAPI;
  },
  beforeDestroy() {
    this.univerAPI.dispose();
    this.univerAPI = null;
  },
}
</script>